#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size();)
        {
            if (nums[i] > 0) break;
            int left = i + 1;
            int right = nums.size() - 1;
            while (left < right)
            {
                if (nums[i] + nums[left] + nums[right] > 0)
                {
                    right--; while (left < right && nums[right] == nums[right + 1])right--;
                }
                else if (nums[i] + nums[left] + nums[right] < 0)
                {
                    left++;
                    while (left < right && nums[left] == nums[left - 1]) left++;
                }
                else {
                    res.push_back({ nums[i],nums[left],nums[right] });
                    left++, right--;
                    while (left < right && nums[left] == nums[left - 1]) left++;
                    while (left < right && nums[right] == nums[right + 1])right--;
                }
            }
            i++;
            while (i < nums.size() && nums[i] == nums[i - 1])i++;
        }
        return res;
    }
};
int main()
{
    vector<int> a;
    a.push_back(-1);
    a.push_back(0);
    a.push_back(1);
    a.push_back(2);
    a.push_back(-1);
    a.push_back(-4);
    vector<vector<int>> ret=Solution().threeSum(a);
    for (int i = 0; i < ret.size(); i++) {
        for (int j = 0; j <= 2; j++)
        {
            cout << ret[i][j]<<' ';
        }
    }
}